package com.tongzhan.service;

import com.tongzhan.base.BaseCriteria;
import com.tongzhan.base.BaseShareService;
import com.tongzhan.consts.SysDictTypeKeyConst;
import com.tongzhan.entity.MarkPrompt;
import com.tongzhan.entity.User;
import com.tongzhan.entity.party.NewPerson;
import com.tongzhan.utils.DateSwitcher;
import com.tongzhan.utils.ThreadLocalUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 发展新员
 * @author htmic
 * @date 2020/7/29
 */
@Service
public class NewPersonService extends BaseShareService {

    /**
     * 获取列表
     * @param type 是否批示  0不是  1是
     * @param page
     * @param size
     * @param year_and_month YYYY-MM 如果是全年：YYYY
     * @return
     */
    public List getList(Integer type, Integer page, Integer size, String userId, String year_and_month) {
        List<Map<String, Object>> list = new ArrayList<>();

        //处理时间查询
        Date startDate = null;
        Date endDate = null;
        if (year_and_month.length()>4){
            //查询年月 YYYY-MM
            Date date = DateSwitcher.string2Date(year_and_month, "yyyy-MM");
            startDate = DateSwitcher.getMonthFirstDay(date);
            endDate = DateSwitcher.getMonthLastDay(date);
        }else{
            //查询年 YYYY
            Date date = DateSwitcher.string2Date(year_and_month, "yyyy");
            startDate = DateSwitcher.getYearFirstDay(date);
            endDate = DateSwitcher.getYearLastDay(date);
        }

        //党派工商科 查看全部
        //未批示(indicate_result;//审核结果   0:未审核、1:同意 2:不同意)
        Page<NewPerson> pageList1 = null;
        if(type == 0){
            Query qw = new Query().addCriteria(BaseCriteria.where("is_del").is(0).and("indicate_result").is(0).and("addtime").gte(startDate).lte(endDate));
            PageRequest of = PageRequest.of(page, size, Sort.Direction.DESC, "addtime");
            if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.STREET_CODE)) {
                qw.addCriteria(BaseCriteria.where("streetCode").like(ThreadLocalUtil.getThreadLocal().getStreetCode()));
            }
            if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.VILLAGE_CODE)) {
                qw.addCriteria(BaseCriteria.where("villageCode").like(ThreadLocalUtil.getThreadLocal().getVillageCode()));
            }
            pageList1 = newPersonRepository.findPage(qw, of);
            pageList1.getContent().forEach(item->{
                item.setPartyTypeName(sysDictService.getValueByType(SysDictTypeKeyConst.PUBLIC_PARTY_TYPE, item.getPartyTypeId()));
            });
        }else{
            Query qw = new Query().addCriteria(BaseCriteria.where("is_del").is(0).and("indicate_result").in(1,2).and("addtime").gte(startDate).lte(endDate));
            PageRequest of = PageRequest.of(page, size, Sort.Direction.DESC, "auditTime");
            if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.STREET_CODE)) {
                qw.addCriteria(BaseCriteria.where("streetCode").like(ThreadLocalUtil.getThreadLocal().getStreetCode()));
            }
            if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.VILLAGE_CODE)) {
                qw.addCriteria(BaseCriteria.where("villageCode").like(ThreadLocalUtil.getThreadLocal().getVillageCode()));
            }
            pageList1 = newPersonRepository.findPage(qw, of);
            pageList1.getContent().forEach(item->{
                item.setPartyTypeName(sysDictService.getValueByType(SysDictTypeKeyConst.PUBLIC_PARTY_TYPE, item.getPartyTypeId()));
            });
        }

        //党派只能看到属于自己的信息
        //未批示(indicate_result;//审核结果   0:未审核、1:同意 2:不同意)
        Page<NewPerson> pageList2 = null;
        if(type == 0){
            Query qw = new Query().addCriteria(BaseCriteria.where("is_del").is(0).and("indicate_result").is(0).and("add_userid").is(userId));
            PageRequest of = PageRequest.of(page, size, Sort.Direction.DESC, "addtime");
            if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.STREET_CODE)) {
                qw.addCriteria(BaseCriteria.where("streetCode").like(ThreadLocalUtil.getThreadLocal().getStreetCode()));
            }
            if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.VILLAGE_CODE)) {
                qw.addCriteria(BaseCriteria.where("villageCode").like(ThreadLocalUtil.getThreadLocal().getVillageCode()));
            }
            pageList2 = newPersonRepository.findPage(qw, of);
            pageList2.getContent().forEach(item->{
                item.setPartyTypeName(sysDictService.getValueByType(SysDictTypeKeyConst.PUBLIC_PARTY_TYPE, item.getPartyTypeId()));
            });
        }else{
            Query qw = new Query().addCriteria(BaseCriteria.where("is_del").is(0).and("indicate_result").in(1,2).and("add_userid").is(userId));
            PageRequest of = PageRequest.of(page, size, Sort.Direction.DESC, "auditTime");
            if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.STREET_CODE)) {
                qw.addCriteria(BaseCriteria.where("streetCode").like(ThreadLocalUtil.getThreadLocal().getStreetCode()));
            }
            if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.VILLAGE_CODE)) {
                qw.addCriteria(BaseCriteria.where("villageCode").like(ThreadLocalUtil.getThreadLocal().getVillageCode()));
            }
            pageList2 = newPersonRepository.findPage(qw, of);
            pageList2.getContent().forEach(item->{
                item.setPartyTypeName(sysDictService.getValueByType(SysDictTypeKeyConst.PUBLIC_PARTY_TYPE, item.getPartyTypeId()));
            });
        }

        Map<String, Object> map1 = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        map1.put("gskList", pageList1.getContent());
        map1.put("total", pageList1.getTotalElements());
        map2.put("dpList", pageList2.getContent());
        map2.put("total", pageList2.getTotalElements());

        list.add(map1);
        list.add(map2);
        return list;
    }

    /**
     * 获取详情
     * @param id
     * @return
     */
    public NewPerson getDetail(String id,String userId) {
        markPromptService.read(id,userId);
        NewPerson people = newPersonRepository.findByPrimaryKey(id);
        if (people != null) {
            people.setPartyTypeName(sysDictService.getValueByType(SysDictTypeKeyConst.PUBLIC_PARTY_TYPE, people.getPartyTypeId()));
        }
        return people;
    }

    /**
     *
     * @param saveModel
     * @return
     */
    public long add(NewPerson saveModel) {
        saveModel.setIs_del(0);
        saveModel.setIndicate_result(0);
        saveModel.setAddtime(new Date());
        User userById = findUserById(saveModel.getAdd_userid());
        if(userById!=null){
            saveModel.setAdd_username(userById.getUserName());
        }
        saveModel.setStreetCode(ThreadLocalUtil.getThreadLocal().getStreetCode());
        saveModel.setVillageCode(ThreadLocalUtil.getThreadLocal().getVillageCode());
        long l = newPersonRepository.insertSelective(saveModel);
        if(l>0){
            //添加角标
            markPromptService.add(new MarkPrompt(6, saveModel.getId()),"11");
            return  1;
        }
        return 0;
    }

    /**
     * 审核
     * @param id
     * @param type 0是不同意 1是同意
     * @param userId 审核人id
     * @return
     */
    public long auditNewPerson(String id, Integer type, String userId) {
        NewPerson newPerson = newPersonRepository.findByPrimaryKey(id);
        if(newPerson!=null){
            User user = findUserById(userId);
            if(user!=null){
                newPerson.setAuditTime(new Date());
                newPerson.setAuditUserId(userId);
                newPerson.setAuditUserName(user.getUserName());
            }
            newPerson.setIndicate_result(type==0?2:1);//审核结果   0:未审核、1:同意 2:不同意
            long l = newPersonRepository.updateByPrimaryKeySelective(newPerson);
            if (l >= 0) {
                //添加角标
                markPromptService.add(new MarkPrompt(1, newPerson.getId()),"12");
                return 1;
            } else {
                return 0;
            }
        }
        return 0;
    }
}
